package com.yanq.liancar.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.yanq.liancar.R;
import com.yanq.liancar.adapter.FuwuAdapter;
import com.yanq.liancar.base.BaseLazyFragment;
import com.yanq.liancar.view.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CGuanjiaFragment extends BaseLazyFragment {


    @BindView(R.id.top)
    LinearLayout toolbar;


    @BindView(R.id.recycler_fuwu)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_fuwu)
    VerticalSwipeRefreshLayout mRefreshLayout;
    private View headView;
    private List<String> mItemList = new ArrayList<>();
    private FuwuAdapter mFuwuAdapter;
    private SwipeRefreshLayout.OnRefreshListener mRefreshListener;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_cguanjia;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImmersionBar.setTitleBar(getActivity(), toolbar);
//添加分割线
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(
//                getContext(), LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.cd9d9d9d)));
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
//        mImmersionBar.statusBarDarkFont(true).init();
    }

    @Override
    protected void initView() {
        mRefreshLayout.setColorSchemeColors(Color.BLUE,
                Color.GREEN,
                Color.YELLOW,
                Color.RED);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        mRefreshLayout.setDistanceToTriggerSync(300);
        // 设定下拉圆圈的背景
        mRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        // 设置圆圈的大小
        mRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
        mRefreshLayout.setRefreshing(false);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(),
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        mFuwuAdapter = new FuwuAdapter();
        mRecyclerView.setAdapter(mFuwuAdapter);
        for (int i = 1; i <= 20; i++) {
            mItemList.add("" + i);
        }
        addHeadView();

        // 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
        mFuwuAdapter.setPreLoadNumber(1);

        mFuwuAdapter.setNewData(mItemList);


    }

    private void addHeadView() {
        headView = LayoutInflater.from(getActivity()).inflate(R.layout.header_fuwu, (ViewGroup) mRecyclerView.getParent(), false);
        mFuwuAdapter.addHeaderView(headView);

    }

    @Override
    protected void setListener() {
        mFuwuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });

        //设置下拉刷新的监听
        mRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mItemList.clear();
                        mItemList.addAll(newData());
                        mFuwuAdapter.setNewData(mItemList);
                        mRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        };
        mRefreshLayout.setOnRefreshListener(mRefreshListener);

        mFuwuAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mFuwuAdapter.addData(addData());
                        if (mItemList.size() == 100) {
                            mFuwuAdapter.loadMoreEnd();
                        } else
                            mFuwuAdapter.loadMoreComplete();
                    }
                }, 2000);
            }
        }, mRecyclerView);

    }

    private List<String> addData() {
        List<String> data = new ArrayList<>();
        for (int i = mItemList.size() + 1; i <= mItemList.size() + 20; i++) {
            data.add("" + i);
        }
        return data;
    }

    private List<String> newData() {
        List<String> data = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            data.add("" + i);
        }
        return data;
    }
}
